import React, { Component } from "react";
import { Link, Route, Switch } from "react-router-dom";

class Home extends Component {
  render() {
    return <div>这是首页页面</div>;
  }
}

const About = () => {
  return <div>这是关于页面</div>;
};

class Detail extends Component {
  render() {
    console.log(this.props.match?.params?.id);
    return <div>这是详情页</div>;
  }
}

class Detail2 extends Component {
  render() {
    console.log(this.props);
    const {
      location: { search },
    } = this.props;
    const obj = new URLSearchParams(search);
    console.log(obj.get("id"));
    return <div>这是详情页2</div>;
  }
}
class App extends Component {
  render() {
    return (
      <>
        <h2>路由query传参</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于页</Link>
          </li>
          <li>
            <Link to="/detail/234">详情页</Link>
          </li>
          <li>
            {/* <Link to="/detail2?id=567&num=100">详情页2</Link> */}
            <Link
              to={{
                pathname: "/detail2",
                search: "?id=567&num=100",
                hash: "#abc",
                // state可以传任意对象,是一种在url上面看不到的一种传参方式
                state: { x: 10, y: 20 },
              }}
            >
              详情页2
            </Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/detail/:id" component={Detail}></Route>
          <Route path="/detail2" component={Detail2}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
